	var year = new Date().getFullYear();
	var month = new Date().getMonth();
	var day = new Date().getDate();

	var PLAN = null;


 	
	 $.ajax({
			url : "/plan",
			async: false,
			type : "GET",
			success  : function(data) {
				PLAN = data;
			} 
	});
	 
	 
	var eventData = {
		events : [
		]
	};
	

	
	  if(PLAN != null) {
		plany = PLAN.split("pauza")
		
	   for (var i = 0; i < plany.length - 1; i++) {
		    var idT = '';
		    var id1 = plany[i].split("{\"data\": [{\"id\" : \"");

		    var id = id1[1];
		    
		    var id2 = id.split("\", \"start\":\"");
		    idT = id2[0]; // id usera
		    var start = id2[1].split("\", \"end\":\"");
		    
		    
		    
		    var startT = start[0];
		    
		    
		    var startTtmp = start[0].split("T");
		    var startTtmp1 = startTtmp[0];
		    var timeTmp = startTtmp[1];
		    
		    
		    var start2 = startTtmp1.split("-");
		    var day = start2[2];
		    var month = start2[1];
		    var year = start2[0];
		    var end = start[1].split("\", \"title\":\" ");
		    var endT = end[0];
		    
		    var tmpEndT = endT.split("T");
		    var tmpEndT2 = tmpEndT[1].split(":");
		    console.log();
		    console.log(tmpEndT2[1]);
		    
		    console.log(end);
		    var title = end[1].split("\"}]}");
		    var titleT  = title[0];
		    
		   eventData.events.push({id: idT, start: new Date(parseInt(year), parseInt(month-1), parseInt(day), 12), end: new Date(parseInt(year), parseInt(month-1), parseInt(day), parseInt(tmpEndT2[0]), parseInt(tmpEndT2[1])), title: titleT});
	   }

	console.log(eventData);
	//jsonObj.push({id: status.options[i].text, optionValue: status.options[i].value});
	
	console.log(eventData);
	  }  
	$(document).ready(function() {
		checked_records = false;
	    $("#achievement-form-id").css("display","none");
	 	if(checked_records==false) {
			getRecords();
			checked_records = true;
		}
	 	
	 	

		$('#calendar').weekCalendar({
			timeslotsPerHour: 4,
			height: function($calendar){
				return $(window).height() - $("h1").outerHeight();
			},
			eventRender : function(calEvent, $event) {
				if(calEvent.end.getTime() < new Date().getTime()) {
					$event.css("backgroundColor", "#aaa");
					$event.find(".time").css({"backgroundColor": "#999", "border":"1px solid #888"});
				}
			},
			eventNew : function(calEvent, $event) {
				displayMessage("<strong>Added event</strong><br/>Start: " + calEvent.start + "<br/>End: " + calEvent.end);
				alert("You've added a new event. You would capture this event, add the logic for creating a new event with your own fields, data and whatever backend persistence you require.");
			},
			eventDrop : function(calEvent, $event) {
				displayMessage("<strong>Moved Event</strong><br/>Start: " + calEvent.start + "<br/>End: " + calEvent.end);
			},
			eventResize : function(calEvent, $event) {
				displayMessage("<strong>Resized Event</strong><br/>Start: " + calEvent.start + "<br/>End: " + calEvent.end);
			},
			eventClick : function(calEvent, $event) {
				displayMessage("<strong>Clicked Event</strong><br/>Start: " + calEvent.start + "<br/>End: " + calEvent.end);
			},
			eventMouseover : function(calEvent, $event) {
				displayMessage("<strong>Mouseover Event</strong><br/>Start: " + calEvent.start + "<br/>End: " + calEvent.end);
			},
			eventMouseout : function(calEvent, $event) {
				displayMessage("<strong>Mouseout Event</strong><br/>Start: " + calEvent.start + "<br/>End: " + calEvent.end);
			},
			noEvents : function() {
				displayMessage("There are no events for this week");
			},
			data:eventData
		});

		function displayMessage(message) {
			$("#message").html(message).fadeIn();
		}

		$("<div id=\"message\" class=\"ui-corner-all\"></div>").prependTo($("body"));
		
	});
	
	
	
	
	
	
	function getRecords() {
		//Czas form
		$( "#achievement-form" ).dialog({
			autoOpen: false,
			height: 300,
			width: 350,
			modal: true,
			buttons: {
				"Zapisz": function() {
					saveFromDialog("achievement");
					$( this ).dialog( "close" );
					location.reload(true);
				
				}
			}
		});
		
		$( "#achievement-form" ).dialog( "open" );
		
	}

